/**
 * Created by Ren on 2015/4/9.
 */
var neusoft = {};
neusoft.matchingGame = {};
neusoft.matchingGame.cardWidth = 80;
neusoft.matchingGame.cardHeigth = 120;
neusoft.matchingGame.deck = [
    "cardAK", "cardAK",
    "cardAQ", "cardAQ",
    "cardAJ", "cardAJ",
    "cardBK", "cardBK",
    "cardBQ", "cardBQ",
    "cardBJ", "cardBJ"
];
var flag = 0;

function shuffle() {
    return Math.random() > 0.5 ? -1 : 1;
}

function checkPattern(cards) {
    var pattern1 = $(cards[0]).data("pattern");
    var pattern2 = $(cards[1]).data("pattern");
/*    alert(pattern1);
    alert(pattern2);*/
    $(cards).removeClass("card-flipped");
    var color = ["red", 'blue', 'yellow', 'black', 'violet'];
    color.sort(shuffle);
    var c = color[0];
/*    alert(c);*/
    $("#time").css({
        "color": c
    });
    if(pattern1 == pattern2) {
        $(cards).addClass("card-removed")
            .bind("webkitTransitionEnd", function(){
                $(this).remove();
            });
        flag ++;
/*        alert(flag);*/
        if(flag == 6) {
/*            alert("ff");*/
            window.location.reload();
        }
    }
}
function selectCard() {
/*        alert($(this).data("pattern"));*/
    var $fcard = $(".card-flipped");
    if($fcard.length > 1) {
        return;
    }
    $(this).addClass("card-flipped");

    var $fcards = $(".card-flipped");
    if($fcards.length == 2) {
///*        alert("hello");*/
        setTimeout(function() {
            checkPattern($fcards);
        },700);
    }
}

$(function(){
    neusoft.matchingGame.deck.sort(shuffle);
/*    alert(neusoft.matchingGame.deck);*/
/*    alert(Math.random());*/
    var $card = $(".card")
    for(var i = 0; i < 11;i++)
    {
        $card.clone().appendTo($("#cards"));
    }

    $(".card").each(function(index) {
/*        alert(index);*/
        $(this).css({
            "left": (neusoft.matchingGame.cardWidth + 20) * (index%4) + "px",
            "top": (neusoft.matchingGame.cardHeigth + 20) * Math.floor(index/4) + "px"
        });

        var pattern = neusoft.matchingGame.deck.pop();
        $(this).data("pattern", pattern);
        $(this).find(".back").addClass(pattern);
        $(this).click(selectCard);
    });
});